package com.fudan.backend.controller;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Order;
import com.fudan.backend.pojo.util.CartOrder;
import com.fudan.backend.pojo.util.IdList;
import com.fudan.backend.pojo.vo.CommodityInfoList;
import com.fudan.backend.service.OrderService;
import com.fudan.backend.util.JwtUtil;
import com.fudan.backend.util.R;
import com.fudan.backend.util.StringUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;
    @Autowired
    StringUtil stringUtil;

    @PostMapping("/preview")
    public R previewOrder(@RequestBody CommodityInfoList commodityInfoList) {
        Map<String, Object> map = orderService.previewOrder(commodityInfoList);
        return new R(RetCode.ORDER_SUCCESS, map);
    }

    /**
     * 直接购买确认订单
     */
    @PostMapping(("/confirm"))
    public R confirmOrderFromSingleCommodity(@RequestBody Map<String, Object> map, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        String addressId;
        String commodityId;
        String moreInfo;
        int commodityNum;
        try {
            addressId = map.get("addressId").toString();
            commodityId = map.get("commodityId").toString();
            moreInfo = map.get("moreInfo").toString();
            commodityNum = Integer.parseInt(map.get("commodityNum").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        Order order = orderService.confirmOrderFromCommodity(commodityId, addressId, commodityNum, moreInfo, userId);
        return new R(RetCode.ORDER_SUCCESS, order);
    }

    @PostMapping("/confirm/cart")
    public R confirmOrderFromCarts(@RequestBody CartOrder cartOrder, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        List<Order> orderList = orderService.confirmOrderFromCart(cartOrder.getCartItemIdList(), cartOrder.getAddressId(), cartOrder.getMoreInfoList(), userId);
        return new R(RetCode.ORDER_SUCCESS, orderList);
    }

    @PostMapping("/pay")
    public R pay(@RequestBody IdList orderIds, @RequestHeader String token) {
        List<String> orderIdList = orderIds.getOrderIdList();
        String userId = JwtUtil.getUserId(token);
        try {
            orderService.payOrder(orderIdList, userId);
        } catch (BaseException e) {
            orderService.payExceptionHandle(e);
            throw e;
        }
        return new R(RetCode.ORDER_SUCCESS);
    }

    @GetMapping("/userorder/{status}")
    public R getUserOrder(int current, int size, @PathVariable("status") int status, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        PageInfo<Order> page = orderService.getUserOrder(userId, status, current, size);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("orderList", page.getList());
        return new R(RetCode.ORDER_SUCCESS, map);
    }

    @PostMapping("/quash/{orderId}")
    public R quashOrder(@PathVariable("orderId") String orderId, @RequestHeader String token) {
        String userId = JwtUtil.getUserId(token);
        RetCode retCode = orderService.quashOrder(orderId, userId);
        return new R(retCode);
    }

    @PostMapping("/delete")
    public R deleteOrder(@RequestBody IdList orderIds, @RequestHeader String token) {
        List<String> orderIdList = orderIds.getOrderIdList();
        String userId = JwtUtil.getUserId(token);
        RetCode retCode = orderService.deleteOrder(orderIdList, userId);
        return new R(retCode);
    }

    @PostMapping("/receipt/{orderId}")
    public R receipt(@PathVariable("orderId") String orderId, @RequestHeader String token){
        String userId = JwtUtil.getUserId(token);
        RetCode retCode = orderService.receiveOrder(orderId, userId);
        return new R(retCode);
    }
}
